<?php

namespace App\Tasks;

use App\Clients\JinShouFu\StoreIncome;
use App\Models\JinshoufuSubAccountTask;
use App\Models\MallDateFinance;
use App\Models\ProjectMall;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\DB;

class JinShouFuIncomeSyncTask extends MeiTuanMallTask
{
    /**
     * @throws GuzzleException
     * @throws Exception
     */
    public function execute(): void
    {
//        MallDateFinance::query()
//            ->where("merchant_no",$this->mall->merchant_no)
//            ->update([
//                "settle_money"=>0
//            ]);
//        return;
        $maxDate = MallDateFinance::query()
            ->where("merchant_no",$this->mall->merchant_no)
            ->max("date");
        if(empty($maxDate)){
            if(!empty($this->account->mall->sub_date)){
                $maxDate = $this->account->mall->sub_date;
            }else{
                $maxDate = "2025-01-01";
            }
            $startDate = $maxDate;
        }else{
            $startDate = date("Y-m-d",strtotime($maxDate) - 1 * 86400);
        }
//        var_dump($maxDate);
//        exit();
        $queryDates = [];

        //$startDate = "2025-07-20";
        $diffDate = (strtotime(date("Y-m-d",time())) - strtotime(date("Y-m-d",strtotime($startDate)))) / 86400;
        foreach (range(0,$diffDate,1) as $index=>$item){
            $beginDate = date("Y-m-d H:i:s",strtotime($startDate) + 1 * $index * 86400);
            $endDate = date("Y-m-d H:i:s",strtotime($beginDate) + 86399);
            $queryDates[] = [
                "tBegin"=>$beginDate,
                "tEnd"=>$endDate,
                "storeId"=>$this->mall->merchant_no,
                "sf_type"=>"2,14,15",
            ];
        }
        $service = new StoreIncome($this->account);
        foreach ($queryDates as $queryDate){
            $date = date("Y-m-d",strtotime($queryDate["tBegin"]));
            $model = MallDateFinance::query()
                ->where("merchant_no",$this->mall->merchant_no)
                ->where("date",$date)
                ->first();
            if(!empty($model) && $model->is_update == 1){
                continue;
            }
            $response = $service->loopExecute($queryDate)->contents();
            $response["income"] = str_replace(",","",$response["income"]);
//            var_dump($response);
//            exit();
            DB::beginTransaction();
            try{
                empty($model) and $model = new MallDateFinance;
                $model->bill_no = $date;
                $model->merchant_no = $this->mall->merchant_no;
                $model->date = $date;
                $model->income = $response["income"];
                $model->settle_money = 0;
                $model->save();
                $subAccountModel = JinshoufuSubAccountTask::query()
                    ->where("begin_date",$date)
                    ->where("end_date",$date)
                    ->first();
                empty($subAccountModel) and $subAccountModel = new JinshoufuSubAccountTask;
                $subAccountModel->begin_date = $date;
                $subAccountModel->end_date = $date;
                $subAccountModel->status = 0;
                $subAccountModel->save();
                DB::commit();
            }catch (Exception $e){
                DB::rollBack();
                throw $e;
            }
        }
        if(!empty($this->account->mall->sub_date)){
            MallDateFinance::query()
                ->where("merchant_no",$this->mall->merchant_no)
                ->where("date","<", $this->account->mall->sub_date)
                ->delete();
        }

        $this->updateIncom();
    }
}
